package com.szyportal.web.back.controller.tool;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Component
public class StaticResourceCopier {

    private final ResourcePatternResolver resourcePatternResolver;

    public StaticResourceCopier(ResourcePatternResolver resourcePatternResolver) {
        this.resourcePatternResolver = resourcePatternResolver;
    }


    public void copyStaticResources(String sourcePath, String targetPath) throws IOException {
        Path source = Paths.get(sourcePath);
        Path target = Paths.get(targetPath);

        // Create target directory if it doesn't exist
        if (!Files.exists(target)) {
            Files.createDirectories(target);
        }

        // Walk through the source directory and copy each file/directory
        Files.walk(source)
                .forEach(sourceFile -> {
                    try {
                        Path targetFile = target.resolve(source.relativize(sourceFile));
                        System.out.println("Copying " + sourceFile + " to " + targetFile);

                        if (Files.isDirectory(sourceFile)) {
                            // Create directory if it doesn't exist
                            if (!Files.exists(targetFile)) {
                                Files.createDirectories(targetFile);
                            }
                        } else {
                            // Copy file, replacing existing file if it exists
                            Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                });
    }

    public void copyStaticResources(String targetPath) throws IOException {
        String staticPath = "classpath:/static/**";
//        String targetPath = "/websites/szyTech/";

        Resource[] resources = resourcePatternResolver.getResources(staticPath);

        for (Resource resource : resources) {
            if (resource.isReadable()) {
                String relativePath = resource.getURL().toString()
                        .substring(resource.getURL().toString().indexOf("/static/") + 8);

                Path targetFilePath = Paths.get(targetPath, relativePath);

                // 创建目录结构
                Files.createDirectories(targetFilePath.getParent());

                // 复制文件
                if (!resource.getURL().toString().endsWith("/")) {
                    Files.copy(resource.getInputStream(), targetFilePath,
                            StandardCopyOption.REPLACE_EXISTING);
                }
            }
        }
    }
    // 递归删除目录的工具方法
    public static void deleteDirectory(File directory) throws IOException {
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        deleteDirectory(file);
                    } else {
                        Files.delete(file.toPath());
                    }
                }
            }
            Files.delete(directory.toPath());
        }
    }
}